package com.ruoyi.quartz.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.system.domain.Qdkb;
import com.ruoyi.system.domain.Yg;
import com.ruoyi.system.domain.Ygrecord;
import com.ruoyi.system.service.IQdkbService;
import com.ruoyi.system.service.IYgService;
import com.ruoyi.system.service.IYgrecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    @Autowired
    private IQdkbService qdkbService;
    @Autowired
    private IYgService ygService;
    @Autowired
    private IYgrecordService ygrecordService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    public void ryInterviewDeadlineTime()
    {
        Date date=new Date();
        LambdaQueryWrapper<Qdkb> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Qdkb::getQdkbId);
        lambdaQueryWrapper.eq(Qdkb::getInterviewdeadline,"N");
        lambdaQueryWrapper.le(Qdkb::getInterviewSendDate,date);
        List<Qdkb> list = qdkbService.list(lambdaQueryWrapper);
        if(list!=null && list.size()!=0){
            for (Qdkb q:list){
                q.setInterviewdeadline("Y");
            }
            qdkbService.updateBatchById(list);
        }

    }
    @Transactional
    public void ryOvertimeElimination(){
        Date date=new Date();
        LambdaQueryWrapper<Yg> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Yg::getYgId,Yg::getStatusTwo,Yg::getStatusThree);
        lambdaQueryWrapper.eq(Yg::getStatusOne,"1");
        lambdaQueryWrapper.in(Yg::getStatusTwo, Arrays.asList("1", "2", "3"));
        lambdaQueryWrapper.le(Yg::getEliminationTime,date);
        List<Yg> list = ygService.list(lambdaQueryWrapper);
        List<Ygrecord> list1 = new ArrayList<>();
        if(list!=null && list.size()!=0){
            for (Yg y:list){
                y.setStatusOne("2");
                Ygrecord ygrecord=new Ygrecord();
                ygrecord.setYgId(y.getYgId());
                ygrecord.setStatusOne(y.getStatusOne());
                ygrecord.setStatusTwo(y.getStatusTwo());
                ygrecord.setStatusThree(y.getStatusThree());
                list1.add(ygrecord);
            }
            ygService.updateBatchById(list);
            ygrecordService.saveBatch(list1);
        }
    }
    @Transactional
    public void ryExpiredAftertimeout(){
        Date date = extendDate(new Date(), 4);
        LambdaQueryWrapper<Yg> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Yg::getYgId,Yg::getStatusTwo,Yg::getStatusThree);
        lambdaQueryWrapper.eq(Yg::getStatusOne,"1");
        lambdaQueryWrapper.eq(Yg::getStatusTwo, "4");
        lambdaQueryWrapper.eq(Yg::getStatusThree,"13");
        lambdaQueryWrapper.le(Yg::getOfferTime,date);
        List<Yg> list = ygService.list(lambdaQueryWrapper);
        List<Ygrecord> list1 = new ArrayList<>();
        if(list!=null && list.size()!=0){
            for (Yg y:list){
                y.setStatusThree("15");
                Ygrecord ygrecord=new Ygrecord();
                ygrecord.setYgId(y.getYgId());
                ygrecord.setStatusOne(y.getStatusOne());
                ygrecord.setStatusTwo(y.getStatusTwo());
                ygrecord.setStatusThree(y.getStatusThree());
                list1.add(ygrecord);
            }
            ygService.updateBatchById(list);
            ygrecordService.saveBatch(list1);
        }

    }

    public Date extendDate(Date date,int days){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -days);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
}
